import java.io.*;
import java.util.ArrayList;

/**
 * menu
 *
 * @author Junye Zhang
 * @date 2024/04/21
 */
public class SaveAndLoad implements Serializable {
    private char[][] board;
    private char[][] enteredLetterBoard;
    private boolean[][] isEnterBoard;
    private boolean firstWord;
    private ArrayList<Player> players;

    /**
     * Instantiates a new Save and load.
     */
    public SaveAndLoad() {
    }

    /**
     * Get entered letter board char [ ] [ ].
     *
     * @return the char [ ] [ ]
     */
    public char[][] getEnteredLetterBoard() {
        return enteredLetterBoard;
    }

    /**
     * Sets entered letter board.
     *
     * @param enteredLetterBoard the entered letter board
     */
    public void setEnteredLetterBoard(char[][] enteredLetterBoard) {
        this.enteredLetterBoard = enteredLetterBoard;
    }

    /**
     * Is first word boolean.
     *
     * @return the boolean
     */
    public boolean isFirstWord() {
        return firstWord;
    }

    /**
     * Sets first word.
     *
     * @param firstWord the first word
     */
    public void setFirstWord(boolean firstWord) {
        this.firstWord = firstWord;
    }

    /**
     * Gets players.
     *
     * @return the players
     */
    public ArrayList<Player> getPlayers() {
        return players;
    }

    /**
     * Sets players.
     *
     * @param players the players
     */
    public void setPlayers(ArrayList<Player> players) {
        this.players = players;
    }

    /**
     * Get is enter board boolean [ ] [ ].
     *
     * @return the boolean [ ] [ ]
     */
    public boolean[][] getIsEnterBoard() {
        return isEnterBoard;
    }

    /**
     * Sets is enter board.
     *
     * @param isEnterBoard the is enter board
     */
    public void setIsEnterBoard(boolean[][] isEnterBoard) {
        this.isEnterBoard = isEnterBoard;
    }


    /**
     * Get board char [ ] [ ].
     *
     * @return the char [ ] [ ]
     */
    public char[][] getBoard() {
        return board;
    }

    /**
     * Sets board.
     *
     * @param board the board
     */
    public void setBoard(char[][] board) {
        this.board = board;
    }


    /**
     * Instantiates a new Save and load.
     *
     * @param board              the letter board
     * @param enteredLetterBoard the entered letter board
     * @param isEnterBoard       the is enterd board
     * @param players            the players
     * @param firstWord          the first word
     */
    public SaveAndLoad(char[][] board, char[][] enteredLetterBoard, boolean[][] isEnterBoard, ArrayList<Player> players, boolean firstWord) {
        this.board = board;
        this.enteredLetterBoard = enteredLetterBoard;
        this.isEnterBoard = isEnterBoard;
        this.players = players;
        this.firstWord = firstWord;
    }

    /**
     * Save game.
     *
     * @param board              the board
     * @param enteredLetterBoard the entered letter board
     * @param isEnterBoard       the is enter board
     * @param players            the players
     * @param firstWord          the first word
     */
    public static void saveGame(char[][] board, char[][] enteredLetterBoard, boolean[][] isEnterBoard, ArrayList<Player> players, boolean firstWord) {
        File f1 = new File("GameSaving.ser");
        if (f1.exists()){
            f1.delete();
        }

        SaveAndLoad sl = new SaveAndLoad(board, enteredLetterBoard, isEnterBoard, players, firstWord);

        try {
            FileOutputStream fos = new FileOutputStream("GameSaving.ser");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(sl);
            fos.close();
            oos.close();
            System.out.println("Game Saved Succeed");
        } catch (IOException e) {
            System.out.println("Game Saved failed");
        }
    }

    /**
     * Load game save and load.
     *
     * @return the save and load
     */
    public static SaveAndLoad loadGame() {
        //search the path to see whether there is a file of the saved game
        File f1 = new File("GameSaving.ser");

        if (!f1.exists()) {
            System.out.println("Can't find your save, please start a new game");
            return null;
        }

        try {
            FileInputStream fis = new FileInputStream("GameSaving.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);

            return(SaveAndLoad) ois.readObject();


        } catch (IOException | ClassNotFoundException e) {

            return null;
        }

    }
}
